package homework1_2;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ThreadPoolCopy implements Runnable {   //实现Runnable接口
    String fromFile;
    String toFile;
    //构造函数传递文件路径信息
    public ThreadPoolCopy(String fromFile, String toFile) {
        this.fromFile = fromFile;
        this.toFile = toFile;
    }

    @Override
    public void run() {
        try {
            //使用Files工具类的copy方法复制文件或目录
            System.out.println(Thread.currentThread().getName() +":"+"CP "+fromFile+"-->"+toFile);
            Files.copy(Paths.get(fromFile), Paths.get(toFile));
            //当目标文件已存在则会文件已存在异常，这是由于多线程调用拷贝函数时出现后进先执行的原因导致的。
        } catch (FileAlreadyExistsException e) {
            System.out.println(Thread.currentThread().getName() +":"+"File Already Exsited!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}